package com.ht.medicaltreatment.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleRecordAdapter
import com.ht.medicaltreatment.bean.SampleRecordItemeBean
import com.ht.medicaltreatment.view.AddressListDialog
import com.ht.medicaltreatment.viewmodel.SampleRecordViewModel
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_sample_record_layout.*

class SampleRecordActivity:BaseActivity(), View.OnClickListener, OnItemClickListener {
    private val model by viewModels<SampleRecordViewModel>()
    private val map = mutableMapOf<String,String>()
    private lateinit var adapter:SampleRecordAdapter
    private var itemList = arrayListOf<String>()

    override fun getLayoutId() = R.layout.activity_sample_record_layout

    override fun init() {
        tvPositive.setOnClickListener(this)
        btnQuery.setOnClickListener(this)
        tvEndTime.setOnClickListener(this)
        tvFangAn.setOnClickListener(this)
        imgFangAn.setOnClickListener(this)

        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = SampleRecordAdapter()
        recyclerView.adapter = adapter
        setTitle("历史接收记录")

        adapter.setOnItemClickListener(this)
        tvStartTime.setOnClickListener(this)
        map["projectId"] = ""
        map["receivetimeend"] = ""
        map["receivetimestart"] = ""
        map["samplename"] = ""
        map["tableName"] = ""

        initData()

        model.fangAn{
            itemList = it
        }
    }

    private fun initData() {
        showProgress()
        model.queryItem(map,{
            dismissProgress()
            adapter.setNewInstance(it)
            tvCheckCount.text = "查询总数: ${it?.size}"
        },{
            dismissProgress()
            showToast(it)
        })
    }

    override fun onClick(v: View?) {
        when(v){
            tvStartTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择开始时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(DateTimeConfig.YEAR,DateTimeConfig.MONTH,DateTimeConfig.DAY))
                    .setOnChoose {millisecond->
                        tvStartTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd")
                    }.build().show()
            }

            tvEndTime -> {
                CardDatePickerDialog.builder(this)
                    .setTitle("选择结束时间")
                    .showBackNow(false)
                    .showFocusDateInfo(false)
                    .setThemeColor(getColor(R.color.blue))
                    .setBackGroundModel(CardDatePickerDialog.CUBE)
                    .setDisplayType(arrayListOf(DateTimeConfig.YEAR,DateTimeConfig.MONTH,DateTimeConfig.DAY))
                    .setOnChoose {millisecond->
                        tvEndTime.text = TimeUtils.millis2String(millisecond,"yyyy-MM-dd")

                    }.build().show()
            }

            btnQuery -> {
                map["receivetimeend"] = tvEndTime.text.toString()
                map["receivetimestart"] = tvStartTime.text.toString()
                map["samplename"] = tvFangAn.text.toString()
                map["tableName"] = etTableName.text.toString()
                initData()
            }

            //选择方案
            tvFangAn,imgFangAn -> {
                AddressListDialog(this).show(itemList){
                    tvFangAn.text = it
                }
            }
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as SampleRecordItemeBean
        val intent = Intent(this,RecordDetailActivity::class.java)
        intent.putExtra("id",item.id)
        startActivity(intent)
    }

}
